package com.hra.hrasittingposture;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.sittingposture.Recognition;
import com.hra.hrasittingposture.service.SittingPostureService;
import com.hra.hrasittingposture.utils.CameraProcess;

import java.util.ArrayList;

public class MainActivity extends Activity {
    private TextView resultTv;
    private Intent si;
    private final CameraProcess cameraProcess = new CameraProcess();

    private SittingPostureService.SittingPostureBinder postureBinder;
    private final ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            postureBinder = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            postureBinder = (SittingPostureService.SittingPostureBinder) service;
            postureBinder.setIsShowDialog(false);
            h.postDelayed(r, 500);
        }
    };

    Handler h = new Handler();
    Runnable r = new Runnable() {
        @Override
        public void run() {
            if (postureBinder != null) {
                ArrayList<Recognition> recognitions = postureBinder.getDetectResult();
                if (recognitions != null && !recognitions.isEmpty()) {
                    String result;
                    for (Recognition res : recognitions) {
                        result = res.getLabelName() + ", " + res.getConfidence();
                        resultTv.setText(result);
                    }
                }
            }
            h.postDelayed(r, 500);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        resultTv = findViewById(R.id.result);
        si = new Intent(this, SittingPostureService.class);

        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 101);
        }
        // 申请摄像头权限
        if (!cameraProcess.allPermissionsGranted(this)) {
            cameraProcess.requestPermissions(this);
            return;
        }
        cameraProcess.showCameraSupportSize(MainActivity.this);
        startService(si);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        startService(si);
    }

    @Override
    protected void onResume() {
        super.onResume();
        bindService(si, mConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            if (postureBinder != null) {
                postureBinder.setIsShowDialog(true);
                unbindService(mConnection);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        h.removeCallbacksAndMessages(null);
    }
}
